package org.jawa.core.util.support;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.dbutils.RowProcessor;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.jawa.core.database.DbConnectionManager;

public class PagingMapListHandler extends MapListHandler {
	/**
	 * 每次查询数量
	 */
	protected int numResults = 10;

	/**
	 * 每次查询数量
	 */
	protected int startIndex = 0;

	public PagingMapListHandler(int startIndex, int pageSize) {
		super();
		this.startIndex = startIndex;
		this.numResults = pageSize;
	}

	public PagingMapListHandler(RowProcessor convert, int startIndex, int pageSize) {
		super(convert);
		this.startIndex = startIndex;
		this.numResults = pageSize;
	}

	public List handle(ResultSet rs) throws SQLException {
		DbConnectionManager.scrollResultSet(rs, startIndex);

		List rows = new ArrayList();
		int count = 0;
		while (rs.next() && count < numResults) {
			rows.add(this.handleRow(rs));
			count++;
		}
		return rows;
	}
}
